//
//  ViewController.m
//  UIHomeWork17_通讯录
//
//  Created by tusm on 16/8/6.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "ContactTableViewCell.h"
#import "DetailViewController.h"
#import "AddContactViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,AddContactViewControllerDelegate>
@property (nonatomic,strong)NSMutableArray *contactArray;
@property (nonatomic,strong)NSMutableArray *contactArray2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"通讯录";
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    [_contactTableView registerNib:[UINib nibWithNibName:@"ContactTableViewCell" bundle:nil] forCellReuseIdentifier:@"pool"];
    _contactTableView.rowHeight = 100;
    _contactArray = [[NSMutableArray alloc] init];
    [self hanldleData1];
    [self handleData2];
    
}

- (void)hanldleData1{

    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    _contactArray = [NSMutableArray arrayWithContentsOfFile:path];

    
}

- (void)handleData2{
        NSDictionary *dic1 = @{@"address":@"辽宁",  @"image":@"1"};
        NSDictionary *dic2 = @{ @"address":@"北京",  @"image":@"2"};
        NSDictionary *dic3 = @{ @"address":@"天津",  @"image":@"3"};
        NSDictionary *dic4 = @{ @"address":@"河北",  @"image":@"4"};
        NSDictionary *dic5 = @{@"address":@"河南",  @"image":@"5"};
        NSDictionary *dic6 = @{@"address":@"湖南",  @"image":@"6"};
        NSDictionary *dic7 = @{@"address":@"湖北",  @"image":@"7"};
        NSDictionary *dic8 = @{@"address":@"浙江",  @"image":@"8"};
        NSDictionary *dic9 = @{@"address":@"广东",  @"image":@"9"};
        NSDictionary *dic10 = @{@"address":@"广西",  @"image":@"10"};
    
        _contactArray2 = @[dic1,dic2,dic3,dic4,dic5,dic6,dic7,dic8,dic9,dic10].mutableCopy;
    for (int i = 0; i < 50; i++) {
        [_contactArray2 addObject:dic1];
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"++++++++++++++++++++++++++++%ld",_contactArray.count);

    return _contactArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    cell.nameLabel.text = _contactArray[indexPath.row][@"name"];
    cell.phoneLabel.text = _contactArray[indexPath.row][@"phoneNumber"];
    cell.contactImageView.image = [UIImage imageNamed:_contactArray2[indexPath.row][@"image"]];
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailViewController *detail = [[DetailViewController alloc] init];
    detail.name = _contactArray[indexPath.row][@"name"];
    detail.phone = _contactArray[indexPath.row][@"phoneNumber"];
    detail.address = _contactArray2[indexPath.row][@"address"];
    detail.imageName = _contactArray2[indexPath.row][@"image"];
    [self.navigationController pushViewController:detail animated:YES ];
    
}
- (void)passValue:(NSDictionary *)dic{
    NSLog(@"++++++++++++++++++%@",dic);
 NSString *nameStr =  [dic objectForKey:@"addName"];
 NSString *phoneStr = [dic objectForKey:@"addPhone"];
    NSString *gender = @"怒";
    NSNumber *num = @12;
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    [dic1 setObject:nameStr forKey:@"name"];
    [dic1 setObject:num forKey:@"age"];
    [dic1 setObject:gender forKey:@"gender"];
    [dic1 setObject:phoneStr forKey:@"phoneNumber"];
    [_contactArray addObject:dic];

    NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
NSString *addressStr = [dic objectForKey:@"addAddress"];
    NSString *image = @"2";
    [dic2 setObject:addressStr forKey:@"address"];
    [dic2 setObject:image forKey:@"image"];
    [_contactArray2 addObject:dic2];
    [_contactTableView reloadData];

}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [_contactTableView setEditing:editing animated:animated];
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_contactArray removeObjectAtIndex:indexPath.row];
    [_contactArray2 removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    
}
- (IBAction)addContactButton:(id)sender {
    
    AddContactViewController *add = [[AddContactViewController alloc] init];
    add.dataSource = self;
    
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
